<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\ShopCoupon;
use Input;

class ShopCouponController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询出所有数据
        $datas = ShopCoupon::select('shops_coupon.cid','shops_coupon.title','shops_coupon.type','shops_coupon.giftname','shops.shopname')
            ->join('shops','shops.shopid','=','shops_coupon.shopid')
            ->paginate('30');
        return view('admin.shopcoupon.index',compact('datas'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //根据id获得数据、、、、
        $datas = ShopCoupon::select('shops_coupon.*','shops.shopname','shops.shopid')
            ->join('shops','shops_coupon.shopid','=','shops.shopid')
            ->where('shops_coupon.cid',$id)
            ->first();
        return view('admin.shopcoupon.show',compact('datas'));




    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //通过id获取信息
        $datas = ShopCoupon::where('cid',$id)->select('*')->first();
        return view('admin.shopcoupon.edit',compact('datas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //将数据进行验证
        $this -> validate($request,[
            'title'=>'required|max:40',
            'giftname'=>'required|max:40',
        ],[
            'title.required'=>'需要添加标题',
            'title.max' => '长度超出',
            'giftname.required'=>'需要添加标题',
            'giftname.max' => '长度超出',
        ]);
        //获取数据
        $cid = $id;
        $token = Input::get('token');
        $title = Input::get('title');
        $type = Input::get('type');
        $value = Input::get('value');
        $giftname = Input::get('giftname');
        $asrule = Input::get('asrule');
        $maxmoney = Input::get('maxmoney');
        $status = Input::get('status');
        //执行更新
        $res = ShopCoupon::where('cid',$cid)->update(['token'=>$token,'title'=>$title,'type'=>$type,'value'=>$value,'giftname'=>$giftname,'asrule'=>$asrule,'maxmoney'=>$maxmoney,'status'=>$status]);
        if($res)
        return back() -> with(['info'=>'更新成功']);
        return back() -> with(['info'=>'更新失败']);

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //获取id
        $id = Input::get('id');
        //删除数据
        $res = ShopCoupon::where('cid',$id)->delete();
        if($res)
        return Response() -> json(['status'=>'success','msg'=>'删除成功']);
        return Response() -> json(['status'=>"error",'msg'=>'删除失败']);

    }
}
